package cn.itcast.java.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Author itcast
 * Date 2021/10/10 16:01
 * 定义一个学生类, 属性为姓名和年龄.
 * 创建List集合, 用来存储学生对象.
 * 往List集合中, 添加3个学生的信息.
 * 分别通过两种遍历方式（普通迭代器 iterator；普通for循环）, 来遍历List集合.
 */
public class ListDemo01 {
    public static void main(String[] args) {
        List<Student> list  = new ArrayList<>();
        list.add(new Student("zhangsan",10));
        list.add(new Student("wangwu",20));
        list.add(new Student("xiaowang",30));
        //循环遍历集合
        //遍历1-iterator
        Iterator<Student> iterator = list.iterator();
        while(iterator.hasNext()){
            Student s = iterator.next();
            System.out.println(s.toString());
        }
        System.out.println("----------------------");
        //遍历2-for循环
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.toString());
        }
    }

    public static class Student{
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}
